package com.xqm.springcloud.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xqm.springcloud.pojo.UserInfo;
import com.xqm.springcloud.mapper.UserInfoMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ShiMing
 * @version 1.0
 * @name UserController
 * @description TODO
 * @date 2020/8/20
 */

@RestController
public class UserController {

    @Autowired
    UserInfoMapper userInfoMapper;

    @GetMapping("/userinfos")
    public List<UserInfo> selectAll() {
        // 直接调用mapper,偷懒了,不要学我
        return userInfoMapper.selectAll();
    }


    @GetMapping("/userinfos/{phone}")
    @HystrixCommand(fallbackMethod = "userInfoHystrix")
    public UserInfo userInfo(@PathVariable String phone){
        UserInfo userInfo = userInfoMapper.selectUser(phone);
        if (userInfo == null){
            throw new RuntimeException();
        }
        return userInfo;

    }

    public UserInfo userInfoHystrix(@PathVariable String phone){
        UserInfo userInfo = new UserInfo();
        userInfo.setUserPhone(phone);
        userInfo.setUserNickname("用户不存在");
        return userInfo;

    }
}
